function busFormatResult(bus) {
	var markup = "<hr><table><tr><th>Placa</th><td>";
	markup += bus.Bus.placa;
	markup += "</td></tr>";
	markup += "<tr><th>Capacidad</th><td>";
	markup += bus.Bus.capacidad;
	markup += "</td></tr>";
	markup += "<tr><th>Empresa</th><td>";
	markup += bus.Bus.empresa;
	markup += "</td></tr>";
	markup += "</table><hr>";
	return markup;
}

function busFormatSelection(bus, container, query) {
	if (bus.length != 0) {
		return bus.Bus.placa;
	}
	return bus;
}

function conductorFormatResult(conductor) {
	var markup = "<hr><table><tr><th>Nombre</th><td>";
	markup += conductor.Conductor.nombre_conductor;
	markup += "</td></tr>";
	markup += "<tr><th>Identificaci&oacute;n</th><td>";
	markup += conductor.Conductor.identificacion_conductor;
	markup += "</td></tr>";
	markup += "</table><hr>";
	return markup;
}

function conductorFormatSelection(conductor) {
	if (conductor.length != 0) {
		return conductor.Conductor.nombre_conductor;
	}
	return conductor;
}

function recorridoFormatResult(recorrido) {
	var markup = "<hr><table><tr><th>Nombre Ruta</th><td>";
	markup += recorrido.Ruta.nombre_ruta;
	markup += "</td></tr>";
	markup += "<tr><th>Descripci&oacute;n Ruta</th><td>";
	markup += recorrido.Ruta.descripcion_ruta;
	markup += "</td></tr>";
	markup += "<hr><table><tr><th>Nombre Horario</th><td>";
	markup += recorrido.Horario.nombre_horario;
	markup += "</td></tr>";
	markup += "<tr><th>D&iacute;as Aplica</th><td>";
	markup += recorrido.Horario.dias_aplica;
	markup += "</td></tr>";
	markup += "<tr><th>Hora Salida</th><td>";
	markup += recorrido.Horario.hora_salida;
	markup += "</td></tr>";
	markup += "</table><hr>";
	return markup;
}

function recorridoFormatSelection(recorrido) {
	if (recorrido.length != 0) {
		return recorrido.Ruta.nombre_ruta +  " - " + recorrido.Horario.nombre_horario;;
	}
	return recorrido;
}

$(document).ready(function() {
	$("#BusIdBus").select2({
		placeholder: "Escoger...",
		width: "off",
		dropdownAutoWidth: true,
		id: function(object) {
			return object.Bus.id_bus;
		},
		initSelection: function(element, callback) {
			var data;
			if (this instanceof Window){
				for (var i in this.app.buses) {
					if (this.app.buses[i]["Bus"]['id_bus'] == $(element).val()){
						data = this.app.buses[i];
						break;
					}
				}
			}
			else {
				for (var i in this.data) {
					if (this.data[i]["Bus"]['id_bus'] == $(element).val()){
						data = this.data[i];
						break;
					}
				}
			}
	        callback(data);
		},
		query: function(options) {
			if (options.term == "" || options.term === undefined) {
				var result = new Object();
				result.results = this.data;
				options.callback(result);
			}
			else {
				var result = new Object();
				result.results = [];
				for (var i in this.data) {
					for (var propiedad in this.data[i]["Bus"]) {
						if (this.data[i]["Bus"][propiedad].toLowerCase().indexOf(options.term.toLowerCase()) != -1) {
							result.results.push(this.data[i]);
							break;
						}
					}
				}
				options.callback(result);
			}
		},
		allowClear: true,
		data: window.app.buses,
		formatResult: busFormatResult,
	    formatSelection: busFormatSelection,
		escapeMarkup: function(m) { return m; }
	});
	$("#BusIdBus").select2("val", $("#BusIdBus").val());

	$("#ConductorIdConductor").select2({
		placeholder: "Escoger...",
		width: "off",
		dropdownAutoWidth: true,
		id: function(object) {
			return object.Conductor.id_conductor;
		},
		initSelection: function(element, callback) {
			var data;
			if (this instanceof Window){
				for (var i in this.app.conductores) {
					if (this.app.conductores[i]["Conductor"]['id_conductor'] == $(element).val()){
						data = this.app.conductores[i];
						break;
					}
				}
			}
			else {
				for (var i in this.data) {
					if (this.data[i]["Conductor"]['id_conductor'] == $(element).val()){
						data = this.data[i];
						break;
					}
				}
			}
	        callback(data);
		},
		query: function(options) {
			if (options.term == "" || options.term === undefined) {
				var result = new Object();
				result.results = this.data;
				options.callback(result);
			}
			else {
				var result = new Object();
				result.results = [];
				for (var i in this.data) {
					for (var propiedad in this.data[i]["Conductor"]) {
						if (this.data[i]["Conductor"][propiedad].toLowerCase().indexOf(options.term.toLowerCase()) != -1) {
							result.results.push(this.data[i]);
							break;
						}
					}
				}
				options.callback(result);
			}
		},
		allowClear: true,
		data: window.app.conductores,
		formatResult: conductorFormatResult,
	    formatSelection: conductorFormatSelection,
		escapeMarkup: function(m) { return m; }
	});

	$("#RecorridoIdRecorrido").select2({
		placeholder: "Escoger...",
		width: "off",
		dropdownAutoWidth: true,
		id: function(object) {
			return object.Recorrido.id_recorrido;
		},
		initSelection: function(element, callback) {
			var data;
			if (this instanceof Window){
				for (var i in this.app.recorridos) {
					if (this.app.recorridos[i]["Recorrido"]['id_recorrido'] == $(element).val()){
						data = this.app.recorridos[i];
						break;
					}
				}
			}
			else {
				for (var i in this.data) {
					if (this.data[i]["Recorrido"]['id_recorrido'] == $(element).val()){
						data = this.data[i];
						break;
					}
				}
			}
	        callback(data);
		},
		query: function(options) {
			if (options.term == "" || options.term === undefined) {
				var result = new Object();
				result.results = this.data;
				options.callback(result);
			}
			else {
				var result = new Object();
				result.results = [];
				for (var i in this.data) {
					for (var propiedad in this.data[i]["Recorrido"]) {
						if (this.data[i]["Recorrido"][propiedad].toLowerCase().indexOf(options.term.toLowerCase()) != -1) {
							result.results.push(this.data[i]);
							break;
						}
					}
				}
				options.callback(result);
			}
		},
		allowClear: true,
		data: window.app.recorridos,
		formatResult: recorridoFormatResult,
	    formatSelection: recorridoFormatSelection,
		escapeMarkup: function(m) { return m; }
	});
});
